;
(function (window, angular) {
	'use strict';
	angular.module("Controller.BasicData.PurchaseType", [])
		.controller("PurchaseTypeCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, UserContextService) {
			$scope.activeFlag = [true, false];
			$scope.submitted = false;
			$scope.dataList = [];
			$scope.detailInfo = {};
			$scope.tabFlag = {};
			var tabFlag = {
				0: "新增",
				1: "编辑"
			};
			$scope.root = {
				id: "0",
				isChecked: false,
				children: []
			};
			var checkedDepartList = [];
			var editingDepart = {};
			$scope.currentDepart = {};

			var btnPermissionCode = {
                purchaseTypeAdd: window.ActionCode.purchaseTypeAdd,
                purchaseTypeDel: window.ActionCode.purchaseTypeEdit,
				purchaseTypeEdit: window.ActionCode.purchaseTypeDel
			};

			$scope.btnPermission = {
				purchaseTypeAdd: true,
				purchaseTypeDel: true,
				purchaseTypeEdit: true
			};

			function setBtnPermission() {
				for (var key in btnPermissionCode) {
					$scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
				}
			}

			$scope.changeTab = function (index) {
				$scope.activeFlag = [false, false];
				$scope.activeFlag[index] = true;
				$scope.submitted = false;
				switch (index) {
					case 0:
						initData();
						break;
					case 1:
						break;
					default:
						break;
				}
			};
			function initData() {
				$scope.tabFlag = {};
				$scope.detailInfo = {};
			}
			$scope.getChildData = function (parent, needFilter) {
				if (parent.children.length > 0 && parent.id !== "0") {
					return;
				}
				var service = "device";
				var reqUrl = "/purchaseDictionary/list";
				var param = {pId: parent.id};
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					angular.forEach(resp.value, function (item) {
						item.isChecked = parent.isChecked;
						item.isParent = (item.childCount > 0);
						item.children = [];
					});
					parent.children = resp.value;
				})
			};

			$scope.delData = function (parentNode, index) {
				var service = "device";
				var reqUrl = "/purchaseDictionary/remove";
				var param = {
					ids:[parentNode.children[index].id]
				};
				ybWidget.ShowMessage("确定要删除吗？", MESG_TYPE.Ask).then(function () {
					ybHttp.Post(service, reqUrl, param).then(function (resp) {
						if (resp.code !== 0) {
							ybWidget.ShowMessage(resp, MESG_TYPE.Error);
							return;
						}
						ybWidget.ShowToaster('success', '操作成功');
						parentNode.children.splice(index, 1);
						if (parentNode.children.length === 0) {
							parentNode.isParent = false;
						}
					})
				}, function () {

				})
			};

			function getAllCheckDepart(node) {
				angular.forEach(node.children, function (item) {
					if (item.isChecked === true) {
						checkedDepartList.push(item.id);
					}
					if (item.children.length > 0) {
						getAllCheckDepart(item);
					}
				})
			}

			function getDetail(service, reqUrl, param) {
				return ybHttp.Get(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return false;
					}
					return resp.value;
				});
			}

			$scope.addSubNode = function (item) {
				$scope.currentDepart = item;
				$scope.detailInfo.pId = item.id;
				$scope.detailInfo.parentNodeName = item.name || "";
				$scope.detailInfo.name = "";
				$scope.detailInfo.code = "";
				$scope.tabFlag.type = 0;
				$scope.tabFlag.name = tabFlag[0];
				$scope.changeTab(1);
			};

			$scope.editData = function (curDepart, pareDepart) {
				editingDepart = curDepart;
				$scope.tabFlag.type = 1;
				$scope.tabFlag.name = tabFlag[1];
				$scope.detailInfo = {
					id: curDepart.id,
					pId: pareDepart.id || "",
                    parentNodeName: pareDepart.name || "",
					name: curDepart.name,
					code: curDepart.code
				};
				$scope.changeTab(1);
			};


			$scope.saveData = function (isValid) {
				if (!isValid) {
					$scope.submitted = true;
					return;
				}
				var service = "device";
				var reqUrl = "/purchaseDictionary/add";
				if ($scope.tabFlag.type === 1) {
					reqUrl = "/purchaseDictionary/update";
				}
				console.info($scope.detailInfo);
				var param = angular.copy($scope.detailInfo);
				ybHttp.Post(service, reqUrl, param).then(function (resp) {
					if (resp.code !== 0) {
						ybWidget.ShowMessage(resp, MESG_TYPE.Error);
						return;
					}
					if ($scope.tabFlag.type === 0) {
						var newDepart = {
							code: $scope.detailInfo.code,
							name: $scope.detailInfo.name,
							id: resp.value,
							childCount: 0,
							pId: $scope.currentDepart.id,
							children: []
						};
						$scope.currentDepart.isParent = true;
						$scope.currentDepart.isExpand = true;
						$scope.currentDepart.children.push(newDepart);
					} else {
						editingDepart.name = $scope.detailInfo.name;
						editingDepart.code = $scope.detailInfo.code;
					}
					ybWidget.ShowToaster('success', '操作成功');
					$scope.changeTab(0);
					$scope.getChildData($scope.root);
				});
			};

			(function () {
				$scope.getChildData($scope.root, true);
				UserContextService.SetBtnPermission(setBtnPermission);
			})();
		}])
	;
})(window, window.angular);